package com.jiaoyou.baojian.fragment;


import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.activity.BaseActivity;
import com.jiaoyou.baojian.activity.LoginRegActivity;
import com.jiaoyou.baojian.activity.RoomActivity;
import com.jiaoyou.baojian.adapter.RoomAdapter;
import com.jiaoyou.baojian.entity.RoomEntity;
import com.jiaoyou.baojian.entity.UserEntity;
import com.jiaoyou.baojian.entity.response.RoomItemResponse;
import com.jiaoyou.baojian.entity.response.RoomListResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.http.response.BaseResponse;
import com.jiaoyou.baojian.utils.Utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import retrofit2.Call;

public class RoomListFragment extends BaseFragment implements BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private static final String ARG_PARAM1 = "param1";
    Unbinder unbinder;

    @BindView(R.id.recyclerview)
    RecyclerView recyclerview;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;

    private RoomAdapter roomAdapter;

    private int page = 1;
    private int pageSize = 10;

    public RoomListFragment() {
    }

    public static RoomListFragment newInstance() {
        RoomListFragment fragment = new RoomListFragment();
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_user_list, container, false);
        unbinder = ButterKnife.bind(this, view);
        recyclerview.setLayoutManager(new LinearLayoutManager(getContext()));
        roomAdapter = new RoomAdapter();
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        recyclerview.setAdapter(roomAdapter);

        roomAdapter.setEmptyView(getLoading());
        roomAdapter.setOnItemClickListener(this);
        roomAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        roomAdapter.isFirstOnly(true);
//        roomAdapter.setOnLoadMoreListener(this, recyclerview);
        swipeRefreshLayout.setOnRefreshListener(this);
    }

    private View getLoading() {
        TextView textView = new TextView(getContext());
        textView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        textView.setGravity(Gravity.CENTER);
        textView.setText("加载中");
        return textView;
    }

    private View getEmptyView() {
        View empty = View.inflate(getActivity(), R.layout.common_empty, null);
        empty.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        return empty;
    }

    @Override
    public void onFirstVisible() {
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeRefreshLayout.setRefreshing(true);
                onRefresh();
            }
        }, 500);
    }

    private void getData() {
        Api.getInstance().getRoomList(page, pageSize, new BaofangCallback<RoomListResponse>(){
            @Override
            public void onFailure(Call<BaseResponse<RoomListResponse>> call, Throwable t) {
                toast("获取数据错误：" + t.getLocalizedMessage());
                if(page != 1){
                    roomAdapter.loadMoreFail();
                }
                swipeRefreshLayout.setRefreshing(false);
            }
            @Override
            public void onSuccess(RoomListResponse roomListResponse) {
                swipeRefreshLayout.setRefreshing(false);
                if(roomListResponse.pageNo >= (roomListResponse.total / pageSize)) {
                    roomAdapter.loadMoreEnd();
                }
                else {
                    roomAdapter.loadMoreComplete();
                    roomAdapter.setOnLoadMoreListener(RoomListFragment.this, recyclerview);
                }
                if(roomListResponse.data == null)
                    return;
                if(page == 1){
                    roomAdapter.setNewData(roomListResponse.data);
                } else {
                    roomAdapter.addData(roomListResponse.data);
                }
                roomAdapter.setEmptyView(getEmptyView());
                roomAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        if(!Utils.isLogin()){
            startActivity(LoginRegActivity.class);
            return;
        }
//        Api.getInstance().getRoomKey(((BaseActivity)getActivity()).getus);
        RoomItemResponse roomItemResponse = (RoomItemResponse) adapter.getData().get(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("room", roomItemResponse);
        startActivity(RoomActivity.class, bundle);
    }

    @Override
    public void onLoadMoreRequested() {
        page++;
        getData();
    }

    @Override
    public void onRefresh() {
        page = 1;
        getData();
    }
}
